$(function() {
	initPage();
	initButton();
});

// 初始化页面
function initPage() {
	//初始化显得是的茶人茶艺在数据库存的type为2
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/tea/order/getOrderListByUserId.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			userId:localStorage.getItem('userid')
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				var str = '';
				var proList = result.data;
				if(proList.length>=1){
					$.each(proList,function(i,n){
						if(n.status == "1"){
							str +='<div class="pro-item "><div class="shop-box"><span>'+n.teaUserAddr.userName+'</span><p class="state fr">待付款</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div class="payOrder" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'">去付款</div><span class="qxOrder" type="'+n.teaProduct.cateid+'" status="'+n.status+'" orderId="'+n.id+'" productid="'+n.teaProduct.productid+'">取消订单</span></div></div>';
						}
						if(n.status == "2"){
/*							str +='<div class="pro-item"><div class="shop-box"><span>'+n.teaFrontusers.userName+'</span><p class="state fr">待发货</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div>提醒发货</div></div></div>';*/
							str +='<div class="pro-item"><div class="shop-box"><span>'+n.teaUserAddr.userName+'</span><p class="state fr">待发货</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"></div></div>';
						}
						if(n.status == "3"){
							/*str +='<div class="pro-item "><div class="shop-box"><span>'+n.teaFrontusers.userName+'</span><p class="state fr">买家已发货</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div>确认收货</div><span>查看物流</span></div></div>';*/
							str +='<div class="pro-item "><div class="shop-box"><span>'+n.teaUserAddr.userName+'</span><p class="state fr">买家已发货</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div class="qxOrder" status="'+n.status+'" orderId="'+n.id+'">确认收货</div></div></div>';
						}
						if(n.status == "4"){
							/*str +='<div class="pro-item "><div class="shop-box"><span>'+n.teaFrontusers.userName+'</span><p class="state fr">交易成功</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div>评价</div><span>删除订单</span></div></div>';*/
							str +='<div class="pro-item "><div class="shop-box"><span>'+n.teaUserAddr.userName+'</span><p class="state fr">交易成功</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div>评价</div><span class="qxOrder" status="'+n.status+'" orderId="'+n.id+'">删除订单</span></div></div>';
						}
					});
					
				}
				$('#teaOrder').html(str);
			} else{
				showErrorMsg(result.data);
			}
		}
	});
}


/**
 * 点击事件
 */
function initButton() {
	$('.order_tab > li').on('click', function() {
		var index = $(this).index();
		$(this).addClass('order_sel').siblings().removeClass('order_sel')
		changePage(index);
		//$(this).addClass('nav-sel').siblings().removeClass('nav-sel');
		//$('.change-item').eq(index).addClass('dis-block').siblings().removeClass('dis-block');
	})	
	$('.evaluate_box').click(function() {
		$('.attention_box').removeClass('disno1');
	})
	$('.box_btn > input').click(function() {
		$('.attention_box').addClass('disno1');
	})
	$("body").on('click','.teatea',function(){
		var param = {};
		param.productId = $(this).attr('productid');
		param.type = $(this).attr('type');
		//跳转
		localStorage.setItem('param',JSON.stringify(param));
		window.location=server_url+"proDetail.do";
	});
	$("body").on('click','.payOrder',function(){
		var param = {};
		param.productId = $(this).attr('productid');
		param.type = $(this).attr('type');
		//跳转
		localStorage.setItem('param',JSON.stringify(param));
		window.location=server_url+"proDetail.do";
	});
	$("body").on('click','.qxOrder',function(){
		var param = {};
		param.productId = $(this).attr('productid');
		param.id = $(this).attr('orderId');
		var status = $(this).attr('status');
		var orderId = param.id;
		param.type = $(this).attr('type');
		//跳转
		$.ajax({
			type : "get",
			url : server_url
					+ 'rest/tea/order/cancleOrder.do?ran=' + Math.random(),
			dataType : 'json',
			async : true,
			data : {
				orderId:orderId,
				status:status
			},
			success : function(result) {
				console.log(result);
				if (result.retCode == '0000') {
					alert("取消成功");
					window.location = server_url + "myOrder.do";
				} else{
					showErrorMsg(result.data);
				}
			}
		});
	});
}

function changePage(index){
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/tea/order/getOrderListByUserId.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			status:index,
			userId:localStorage.getItem('userid')
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				var str = '';
				var proList = result.data;
				if(proList.length>=1){
					$.each(proList,function(i,n){
						/*if(n.status == "1"){
							str +='<div class="pro-item"><div class="shop-box"><span>'+n.teaFrontusers.userName+'</span><p class="state fr">待付款</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div>去付款</div><span>取消订单</span></div></div>';
						}
						if(n.status == "2"){
							str +='<div class="pro-item"><div class="shop-box"><span>'+n.teaFrontusers.userName+'</span><p class="state fr">待发货</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div>提醒发货</div></div></div>';
						}
						if(n.status == "3"){
							str +='<div class="pro-item "><div class="shop-box"><span>'+n.teaFrontusers.userName+'</span><p class="state fr">买家已发货</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div>确认收货</div><span>查看物流</span></div></div>';
						}
						if(n.status == "4"){
							str +='<div class="pro-item "><div class="shop-box"><span>'+n.teaFrontusers.userName+'</span><p class="state fr">交易成功</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div>评价</div><span>删除订单</span></div></div>';
						}*/
						if(n.status == "1"){
							str +='<div class="pro-item "><div class="shop-box"><span>'+n.teaUserAddr.userName+'</span><p class="state fr">待付款</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div class="payOrder" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'">去付款</div><span class="qxOrder" type="'+n.teaProduct.cateid+'" status="'+n.status+'" orderId="'+n.id+'" productid="'+n.teaProduct.productid+'">取消订单</span></div></div>';
						}
						if(n.status == "2"){
							str +='<div class="pro-item"><div class="shop-box"><span>'+n.teaUserAddr.userName+'</span><p class="state fr">待发货</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"></div></div>';
						}
						if(n.status == "3"){
							str +='<div class="pro-item "><div class="shop-box"><span>'+n.teaUserAddr.userName+'</span><p class="state fr">买家已发货</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div class="qxOrder" status="'+n.status+'" orderId="'+n.id+'">确认收货</div></div></div>';
						}
						if(n.status == "4"){
							str +='<div class="pro-item "><div class="shop-box"><span>'+n.teaUserAddr.userName+'</span><p class="state fr">交易成功</p></div><div class="pro-box"><div class="pro-list clear teatea" type="'+n.teaProduct.cateid+'" productid="'+n.teaProduct.productid+'"><img src="'+n.teaProduct.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+n.teaProduct.productname+'</p><p class="num green-w">¥'+n.perunit+'</p></div></div></div><div class="num-box clear"><div class="all-num fr">共<span class="num-pro">'+n.conut+'</span>件商品 小计：<div class="buy-num">￥<span>'+n.totalunit+'</span></div>（运费:￥<span>'+n.freight+'</span>）</div></div><div class="total-box"><div>评价</div><span class="qxOrder" status="'+n.status+'" orderId="'+n.id+'">删除订单</span></div></div>';
						}
					});
					
				}
				$('#teaOrder').html(str);
			} else{
				showErrorMsg(result.data);
			}
		}
	});
}
